package primerdb;

/**
 *
 * @author Amjad_Almahairi
 */


import java.io.IOException;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.util.LinkedList;
import java.util.logging.Level;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;
import primerdb.persistentTree.DynamicTreeNode;
 
public class XMLImporter {
 
    Document doc;
    File docFile;
    Element rootElement;
    LinkedList<QueueElement> folderQueue;
    
    
    primerdb.persistentTree.JPersistentTree folderTree;
    PrimerDao db;
    
    public XMLImporter(File docFile, primerdb.persistentTree.JPersistentTree folderTree, PrimerDao db) throws ParserConfigurationException, SAXException, IOException {
        this.docFile = docFile;
        this.folderTree = folderTree;
        this.db = db;
       
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            doc = docBuilder.parse(docFile);
            //doc.getDocumentElement().normalize();
            rootElement = doc.getDocumentElement();
            folderQueue = new LinkedList<QueueElement>();

       
    }
    
    public void importFile(){
        NodeList nList = rootElement.getChildNodes();
//        NodeList nList = importer.doc.getElementsByTagName("Folder");
        for (int i = 0; i < nList.getLength(); i++) {
            Node node = nList.item(i);
            if (node instanceof Element) {
                Element child = (Element) node;
                //System.out.println(child.getTagName());
                folderQueue.addLast(new QueueElement(child, null));
            }
        }
        
        while(!folderQueue.isEmpty()){
            QueueElement qe = folderQueue.removeFirst(); 
            Element folder = qe.folder;
            
            //System.out.println("Folder "+folder.getAttribute("name"));
            Folder f = new Folder(folder.getAttribute("name"));
            DynamicTreeNode parentNode = qe.parentNode;
            DynamicTreeNode newNode = folderTree.createInSelectedNode(f,parentNode);
            
            
            NodeList nodes = folder.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                if (nodes.item(i) instanceof Element) {
                    Element child = (Element) nodes.item(i);
                    if ("Folder".equals(child.getTagName())) {
                        folderQueue.addLast(new QueueElement(child, newNode));
                    } else {
                        importPrimer(child, newNode);
                    }
                }
            }
        }
    }
    
    
    private void importPrimer(Element primer, DynamicTreeNode newNode){
        
        int folderId = ((Folder) newNode.getUserObject()).getId();
        
        String name = primer.getElementsByTagName("Name").item(0).getTextContent();
        String sequence = primer.getElementsByTagName("Sequence").item(0).getTextContent();
        String annTemp = primer.getElementsByTagName("AnnTemp").item(0).getTextContent();
        String freezer = primer.getElementsByTagName("Freezer").item(0).getTextContent();
        String target = primer.getElementsByTagName("Target").item(0).getTextContent();
        String conc = primer.getElementsByTagName("Conc").item(0).getTextContent();
        String date = primer.getElementsByTagName("Date").item(0).getTextContent();
        String label = primer.getElementsByTagName("Label").item(0).getTextContent();
        String notes = primer.getElementsByTagName("Notes").item(0).getTextContent();
        String project = primer.getElementsByTagName("Project").item(0).getTextContent();
        
        Primer p = new Primer(name, annTemp, freezer, target, conc, date, label, sequence, notes, project, folderId);
        db.newPrimer(p);
        //System.out.println(primer.getTagName()+" "+name+" "+sequence);
      
    }
    
    private class QueueElement{
        Element folder;
        DynamicTreeNode parentNode;

        public QueueElement(Element folder, DynamicTreeNode parentNode) {
            this.folder = folder;
            this.parentNode = parentNode;
        }
        
        
    }
    
    public static void main(String argv[]) {
//        XMLImporter importer = new XMLImporter(new File("primers.xml"),null);
//        importer.importFile();
         

        
        
        
//            if (node.getNodeType() == Node.ELEMENT_NODE) {
//                NamedNodeMap attribs = node.getAttributes();
//                System.out.print(attribs.getNamedItem("id").getTextContent()+" "); 
//                System.out.print(attribs.getNamedItem("name").getTextContent()+" ");
//                
//                if (node.getParentNode().getNodeName() == "PrimersDB")
//                    System.out.println("");
//                else{
//                    //System.out.println(node.getParentNode().getNodeName());
//                    System.out.println(node.getParentNode().getAttributes().getNamedItem("name").getTextContent());
//                }
//                //element.getElementsByTagName(null)
//                
//            }
//            
//            
//        }
    }
    
    
 
}